﻿using BaseData.ApplicationLib.Commands;
using BaseData.ApplicationLib.Queries;
using BaseData.CommonLib.Constants;
using BaseData.CommonLib.Models;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace BaseData.WebApi.Controllers
{
    [ApiController]
    public class CalculateUnitController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ICalculateUnitQueries _unitQueries;

        public CalculateUnitController(IMediator mediator,
            ICalculateUnitQueries unitQueries)
        {
            _mediator = mediator;
            _unitQueries = unitQueries;
        }

        [HttpGet(ApiRoutes.CalculateUnit.QueryCalculateUnits)]
        public async Task<IActionResult> Get([FromQuery] CalculateUnitSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var result = await _unitQueries.QueryUnitsByPage(model);
                return Ok(result);
            }
            else
            {
                var result = await _unitQueries.QueryUnits(model);
                return Ok(result);
            }
        }

        [HttpGet(ApiRoutes.CalculateUnit.GetCalculateUnit)]
        public async Task<IActionResult> GetUnit(int id)
        {
            var result = await _unitQueries.GetUnit(id);
            return Ok(result);
        }

        [HttpPost(ApiRoutes.CalculateUnit.CreateCalculateUnit)]
        public async Task<IActionResult> Post(CreateCalculateUnitCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpPut(ApiRoutes.CalculateUnit.UpdateCalculateUnit)]
        public async Task<IActionResult> Put(int id, [FromBody] UpdateCalculateUnitCommand command)
        {
            command.Id = id;
            var result = await _mediator.Send(command);
            return Ok(result);
        }

    }
}